﻿using System;
using System.Linq;
using System.Web.Security;
using GalaxyTranslator.EntityDataModel;
using GalaxyTranslator.Model;

namespace GalaxyTranslator.MyMembership
{
	public class MyRoleProvider : RoleProvider
	{
		public override bool IsUserInRole(string username, string roleName)
		{
			MyUser user = DataLayer.GetUserByName(username);
			if (user == null)
				return false;
			return user.Role.Name == roleName;
		}

		public override string[] GetRolesForUser(string username)
		{
			MyUser user = DataLayer.GetUserByName(username);
			if (user == null)
				return new string[0];
			return new[] { user.Role.Name };
		}

		public override void CreateRole(string roleName)
		{
			if (string.IsNullOrWhiteSpace(roleName))
				throw new ArgumentNullException("roleName");
			using (var context = new GalaxyTranslatorEntities())
			{
				Role role = context.Roles.FirstOrDefault(r => r.Name == roleName);
				if (role != null)
					return;
				context.Roles.Add(new Role {Name = roleName});
				context.SaveChanges();
			}
		}

		public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
		{
			if (string.IsNullOrWhiteSpace(roleName))
				return false;
			using (var context = new GalaxyTranslatorEntities())
			{
				Role role = context.Roles.FirstOrDefault(r => r.Name == roleName);
				if (role == null)
					return false;
				if (throwOnPopulatedRole)
				{
					if (role.Users.Any())
						return false;
				}
				context.Roles.Remove(role);
				context.SaveChanges();
				return true;
			}
		}

		public override bool RoleExists(string roleName)
		{
			using (var context = new GalaxyTranslatorEntities())
			{
				return context.Roles.Any(r => r.Name == roleName);
			}
		}

		public override void AddUsersToRoles(string[] usernames, string[] roleNames)
		{
			using (var context = new GalaxyTranslatorEntities())
			{
				foreach (var roleName in roleNames)
				{
					Role role = context.Roles.FirstOrDefault(r => r.Name == roleName);
					if (role == null)
						continue;
					foreach (var userName in usernames)
					{
						MyUser user = context.MyUsers.FirstOrDefault(u => u.Login == userName);
						if (user == null)
							continue;
						user.Role = role;
					}
				}
				context.SaveChanges();
			}
		}

		public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
		{
			throw new NotImplementedException();
		}

		public override string[] GetUsersInRole(string roleName)
		{
			using (var context = new GalaxyTranslatorEntities())
			{
				var role = context.Roles.FirstOrDefault(r => r.Name == roleName);
				if (role == null)
					return new string[0];
				return role.Users.Select(u => u.Login).ToArray();
			}
		}

		public override string[] GetAllRoles()
		{
			using (var context = new GalaxyTranslatorEntities())
			{
				return context.Roles.Select(r => r.Name).ToArray();
			}
		}

		public override string[] FindUsersInRole(string roleName, string usernameToMatch)
		{
			// not implemented
			return new string[0];
		}

		public override string ApplicationName
		{
			// not implemented
			get { return null; }
			set { }
		}
	}
}